<template>
  <div>
    <h1>部门管理</h1>
    <el-row>
      <el-col :span="2"><div>部门名称：</div></el-col>
      <el-col :span="4"
        ><div>
          <el-input
            v-model="name"
            placeholder="请输入内容"
            size="mini"
          ></el-input></div
      ></el-col>
      <el-col :span="2"><div></div></el-col>
      <el-col :span="4"
        ><el-button size="mini" type="primary" @click="GetShow">查询</el-button
        ><el-button size="mini" @click="Reset">重置</el-button>
        <div></div
      ></el-col>
      <el-col :span="4"><div></div></el-col>
      <el-col :span="4"><div></div></el-col>
    </el-row>
    <el-row>
      <el-col :span="4"
        ><div>
          <el-button type="primary" @click="dialogFormVisible = true"
            >添加</el-button
          >
          <el-button type="primary" @click="DelEmployees">删除</el-button>
        </div></el-col
      >
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="Department_Name" label="部门名称" width="150">
      </el-table-column>
      <el-table-column prop="name" label="上级部门" width="150">
      </el-table-column>
      <el-table-column prop="Department_DateTimes" label="创建时间" width="150">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageindex"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalcount"
    >
    </el-pagination>

    <el-dialog title="新增部门" :visible.sync="dialogFormVisible">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="部门名称名称" prop="Department_Name">
          <el-input v-model="ruleForm.Department_Name"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="Department_Superior">
          <el-select
            v-model="ruleForm.Department_Superior"
            placeholder="请选择"
          >
            <el-option
              v-for="item in depment"
              :key="item.Id"
              :label="item.Department_Name"
              :value="item.Id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button
          >
          <el-button @click="dialogFormVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="修改部门" :visible.sync="dialogFormVisibles">
      <el-form
        :model="ruleForms"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="部门名称" prop="Department_Name">
          <el-input v-model="ruleForms.Department_Name"></el-input>
        </el-form-item>
        <el-form-item label="上级部门" prop="Department_Superior">
          <el-select
            v-model="ruleForms.Department_Superior"
            placeholder="请选择"
          >
            <el-option
              v-for="item in depment"
              :key="item.Id"
              :label="item.Department_Name"
              :value="item.Id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForms">立即创建</el-button>
          <el-button @click="dialogFormVisibles = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      pageindex: 1,
      pagesize: 5,
      totalcount: 0,
      name: "",
      multipleSelection: [],
      ids: [],
      depment: [],
      data: [],
      dialogFormVisible: false,
      dialogFormVisibles: false,
      ruleForm: {},
      ruleForms: {},
      rules: {
        Department_Name: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
        ],
        Department_Superior: [
          { required: true, message: "请选择上级部门", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    GetShow() {
      this.$axios
        .get(
          "https://localhost:44360/api/app/t-mS_Department/department-dto?name=" +
            this.name +
            "&pageindex=" +
            this.pageindex +
            "&pagesize=" +
            this.pagesize
        )
        .then((res) => {
          this.tableData = res.data.PageData;
          this.totalcount = res.data.PageTotal;
        });
    },
    //上级部门
    Getdepartment() {
      this.$axios
        .get("https://localhost:44360/api/app/t-mS_Employees-sevice/department")
        .then((res) => {
          this.depment = res.data;
        });
    },
    handleEdit(row) {
      this.ruleForms = row;
      this.dialogFormVisibles = true;
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete(
              "https://localhost:44360/api/app/t-mS_Department/" +
                row.Id +
                "/department"
            )
            .then((res) => {
              if (res.data > 0) {
                alert("删除成功");
                this.GetShow();
              } else {
                alert("删除失败");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    DelEmployees() {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .delete(
              "https://localhost:44360/api/app/t-mS_Department/" +
                this.ids.toString() +
                "/department"
            )
            .then((res) => {
              if (res.data > 0) {
                alert("删除成功");
                this.GetShow();
              } else {
                alert("删除失败");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios
            .post(
              "https://localhost:44360/api/app/t-mS_Department/department",
              this.ruleForm
            )
            .then((res) => {
              if (res.data > 0) {
                alert("添加成功");
                this.dialogFormVisible = false;
                this.GetShow();
              } else {
                alert("添加失败");
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitForms() {
      this.$axios
        .put(
          "https://localhost:44360/api/app/t-mS_Department/department-dto",
          this.ruleForms
        )
        .then((res) => {
          if (res.data > 0) {
            alert("修改成功");
            this.dialogFormVisibles = false;
            this.GetShow();
          } else {
            alert("修改失败");
          }
        });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.multipleSelection.forEach((item) => {
        this.ids.push(item.Id);
      });
    },
    handleSizeChange(val) {
      this.pagesize = val;
      this.GetShow();
    },
    handleCurrentChange(val) {
      this.pageindex = val;
      this.GetShow();
    },
    Reset() {
      this.name = "";
    },
  },
  created() {
    this.GetShow();
    this.Getdepartment();
  },
  computed: {},
};
</script>
<style scoped>
</style>